package com.pj.point.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.pj.point.R;
import com.pj.point.pri.Private;
import com.pj.point.pri.SettingPrivatePwd;
import com.pj.point.util.alert.AlertUtil;


public class PrivateFragment extends Fragment {

    Activity activity;
    private SwipeRefreshLayout privateRefresh;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_private, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        privateRefresh = (SwipeRefreshLayout) view.findViewById(R.id.private_refresh);
        privateRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                SharedPreferences data = activity.getSharedPreferences("data", Context.MODE_PRIVATE);
                if (data.contains("PriPwd")){
                    AlertUtil.show(activity,"输入密码来验证身份",password -> {
                        if (password.equals(""))
                        {
                            Toast.makeText(activity, "请输入密码已验证身份", Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                            if(password.equals(activity.getSharedPreferences("data", Context.MODE_PRIVATE).getString("PriPwd","")))
                            {
                                Intent intent = new Intent(getContext(), Private.class);
                                startActivity(intent);
                                privateRefresh.setRefreshing(false);
                            }
                            else {
                                Toast.makeText(getContext(), "密码错误", Toast.LENGTH_SHORT).show();
                            }

                        }
                    });
                }else{
                    Toast.makeText(activity, "请先设置抽屉密码", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(activity, SettingPrivatePwd.class));
                }
                privateRefresh.setRefreshing(false);
            }
        });
    }
}